import 'package:flutter/material.dart';
import 'package:gd_assistant/api/apiHandler.dart';
import 'package:gd_assistant/model/extensionModel.dart';
import 'package:gd_assistant/page/public/colors.dart';
import 'package:gd_assistant/page/public/publicView.dart';
import 'package:gd_assistant/page/public/publicItem.dart';
import 'package:gd_assistant/page/public/themeUtils.dart';
import 'package:gd_assistant/page/user/appointmentInfo.dart';
import 'package:gd_assistant/util/customRoute.dart';

class userAppointentPage extends StatefulWidget {
  userAppointentPage({Key key, 
                      this.userid, 
                      this.orderid, 
                      this.userName,
                      this.age,
                      this.sex,
                      this.isExpire = false}):super(key: key);
  String userid = '';
  String orderid = '';
  String userName = '';
  String age = '';
  String sex = '';
  bool isExpire; //是否过期
  @override
  _userAppointentPageState createState() => _userAppointentPageState();
}

class _userAppointentPageState extends State<userAppointentPage> {
  bool isDark = false;
  List<Extension> _appointments = []; 
  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() async {
    _appointments = await apiHandler.getAppointmentOrderList(widget.userid, widget.orderid);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    isDark = ThemeUtils.isDark(context);
    final EdgeInsets padding = MediaQuery.of(context).padding;
    return Scaffold(
      appBar: AppBar(
        title: Text("${widget.userName}的预约"),
        elevation: 0
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: ListView.builder(
                itemBuilder: (BuildContext cext, int index){
                  return appointmentItem(
                          appointment: _appointments[index],
                          onTap: (){
                            Navigator.of(context).push(
                              CustomRoute(appointmentInfoPage(exten: _appointments[index],userid: widget.userid,callBack: (){ loadData(); }),routeName: "/appointmentInfo")
                            ); 
                          },);
                },
                itemCount: _appointments.length,
              )
            ),
            Offstage(
              offstage: widget.isExpire,
              child: Container(
              padding: EdgeInsets.only(bottom: padding.bottom),
              width: double.infinity,
              height: 65 + padding.bottom,
              color: isDark ? Colours.dark_bg_color : Colors.white,  
                child: ZLButton(
                  title: "新增预约",
                  onTap: (){
                    print("新增预约");
                    Navigator.of(context).push(
                      CustomRoute(appointmentInfoPage(age: widget.age,
                      userid: widget.userid, sex: widget.sex, userName: widget.userName, packageOrderId: widget.orderid, callBack: (){ loadData(); }),routeName: "/appointmentInfo")
                    );     
                  },
                ),
              ),
            )
          ],
        )
      ),
    );
  }
}